package com.zjsx.lib.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.telephony.TelephonyManager;

public class SysInfo {

    public static String getVersion(Context context) {
	return getPackageInfo(context).versionName;
    }

    public static String getPackageName(Context context) {
	return getPackageInfo(context).packageName;
    }

    public static int getVersionCode(Context context) {
	return getPackageInfo(context).versionCode;
    }

    public static PackageInfo getPackageInfo(Context context) {
	try {
	    return context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
	} catch (Exception e) {
	    throw new RuntimeException("没有找到包名");
	}
    }

    public static Map<String, String> collectDeviceInfo(Context ctx) {
	Map<String, String> infos = new HashMap<String, String>(); // 用来存储设备信息和异常信息
	infos.put("versionName", getVersion(ctx));
	infos.put("versionCode", String.valueOf(getVersionCode(ctx)));
	Field[] fields = Build.class.getDeclaredFields();
	for (Field field : fields) {
	    try {
		field.setAccessible(true);
		infos.put(field.getName(), field.get(null).toString());
		L.d(field.getName() + " : " + field.get(null));
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	return infos;
    }

    public static String getIMEI(Context ctx) {
	TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
	return tm.getDeviceId();
    }
    
    public static String getSysVersion(){
	return android.os.Build.VERSION.RELEASE;
    }
    
    public static String getPhoneModel(){
	return android.os.Build.MODEL;
    }
}
